use codegraph_lsp::{LspServerManager, LspManagerConfig, LspServerStatus};
use std::time::Duration;
use tokio::time::timeout;

// 简化的测试，不依赖有问题的其他crate

#[tokio::test]
async fn test_lsp_manager_creation() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    // 测试管理器创建成功
    assert!(manager.start().await.is_ok());
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_server_discovery() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 测试文件扩展名发现
    let python_server = manager.discover_server_for_file("test.py").await;
    assert_eq!(python_server, Some("python".to_string()));
    
    let rust_server = manager.discover_server_for_file("test.rs").await;
    assert_eq!(rust_server, Some("rust".to_string()));
    
    let unknown_server = manager.discover_server_for_file("test.unknown").await;
    assert_eq!(unknown_server, None);
    
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_server_lifecycle() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 测试启动服务器（这里会失败，因为实际的LSP服务器可能不存在）
    // 但我们可以测试管理器的逻辑
    let result = manager.start_server("python").await;
    // 由于没有实际的pylsp命令，这应该会失败
    assert!(result.is_err());
    
    // 测试获取状态
    let status = manager.get_server_status("python").await;
    if let Some(status) = status {
        // 如果有状态，应该是错误状态
        match status {
            LspServerStatus::Error(_) => {
                // 这是预期的，因为没有实际的LSP服务器
            }
            _ => {
                // 其他状态也是可能的
            }
        }
    }
    
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_multiple_servers() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 尝试启动多个服务器
    let _python_result = manager.start_server("python").await;
    let _rust_result = manager.start_server("rust").await;
    let _typescript_result = manager.start_server("typescript").await;
    
    // 获取所有服务器状态
    let all_status = manager.get_all_server_status().await;
    
    // 应该有一些服务器状态记录
    // 即使启动失败，也应该有错误状态记录
    
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_config_operations() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config.clone());
    
    // 测试获取配置
    let retrieved_config = manager.get_config().await;
    assert_eq!(retrieved_config.global_settings.enable_diagnostics, config.global_settings.enable_diagnostics);
    
    // 测试更新配置
    let mut new_config = config.clone();
    new_config.global_settings.enable_diagnostics = false;
    manager.update_config(new_config.clone()).await;
    
    let updated_config = manager.get_config().await;
    assert_eq!(updated_config.global_settings.enable_diagnostics, false);
}

#[tokio::test]
async fn test_server_restart() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 尝试重启一个不存在的服务器
    let restart_result = manager.restart_server("python").await;
    // 这应该会尝试启动服务器，即使之前没有运行
    
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_manager_shutdown() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    // 启动管理器
    assert!(manager.start().await.is_ok());
    
    // 尝试启动一些服务器
    let _result1 = manager.start_server("python").await;
    let _result2 = manager.start_server("rust").await;
    
    // 停止管理器应该清理所有服务器
    assert!(manager.stop().await.is_ok());
    
    // 停止后的状态检查
    let all_status = manager.get_all_server_status().await;
    // 停止后应该没有服务器状态或者都是停止状态
}

#[tokio::test]
async fn test_concurrent_operations() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 并发启动多个服务器
    let handles = vec![
        tokio::spawn({
            let manager = &manager;
            async move { manager.start_server("python").await }
        }),
        tokio::spawn({
            let manager = &manager;
            async move { manager.start_server("rust").await }
        }),
        tokio::spawn({
            let manager = &manager;
            async move { manager.start_server("typescript").await }
        }),
    ];
    
    // 等待所有操作完成
    for handle in handles {
        let _result = handle.await;
    }
    
    assert!(manager.stop().await.is_ok());
}

#[tokio::test]
async fn test_timeout_handling() {
    let config = LspManagerConfig::default();
    let manager = LspServerManager::new(config);
    
    assert!(manager.start().await.is_ok());
    
    // 测试操作超时
    let result = timeout(Duration::from_secs(5), manager.start_server("python")).await;
    
    // 操作应该在超时时间内完成（即使失败）
    assert!(result.is_ok());
    
    assert!(manager.stop().await.is_ok());
}
